package com.tripupdater.view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Elastic;
	import com.greensock.easing.Linear;
	import com.greensock.easing.Strong;
	import com.tripupdater.controller.Controller;
	import com.tripupdater.event.TUEvent;
	import com.tripupdater.event.TUEventDispatcher;
	import com.tripupdater.model.Model;
	import com.tripupdater.model.Params;
	import com.tripupdater.type.CommandType;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.Security;
	
	public class TripUpdaterMainView extends MovieClip
	{
		private var controller:Controller;
		private var evtDsp:TUEventDispatcher;
		private var model:Model;
		
		private var loading:MovieClip;
		private var sobres:MovieClip;
		private var polaroid:MovieClip;
		private var foto:MovieClip;
		private var about:MovieClip;
		private var spot:MovieClip;
		private var aboutPage:MovieClip;
		
		private var passwordView:PasswordView;
		private var altTwitterView:TwitterView;
		private var twitterView:TwitterView;
		private var calendarView:CalendarView;
		private var mapView:MapView;
		private var skypeView:SkypeView;
		
		public function TripUpdaterMainView()
		{
			Params.BASE_PATH= this.loaderInfo.parameters["basePath"] || "";
			
			Security.allowDomain("*");
			
			stage.stageFocusRect= false;
			evtDsp= TUEventDispatcher.getInstance();
			controller= new Controller(evtDsp);

			model= Model.getInstance();
						
			createChildren();
		}
		
		private function createChildren():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, createChildren);
			
			loading= getChildByName("loadingInstance") as MovieClip;
			sobres= getChildByName("sobresInstance") as MovieClip;
			polaroid= getChildByName("polaroidInstance") as MovieClip;
			foto= getChildByName("fotoInstance") as MovieClip;
			spot= getChildByName("spotInstance") as MovieClip;
			about= getChildByName("aboutInstance") as MovieClip;
			aboutPage= getChildByName("aboutPageInstance") as MovieClip;
			
			passwordView= getChildByName("passwordInstance") as PasswordView;
			altTwitterView= getChildByName("altTwitterInstance") as TwitterView;
			twitterView= getChildByName("twitterInstance") as TwitterView;
			calendarView= getChildByName("calendarInstance") as CalendarView;
			mapView= getChildByName("mapInstance") as MapView;
			skypeView= getChildByName("skypeInstance") as SkypeView;
			
			sobres.mouseChildren= false;
			sobres.mouseEnabled= false;
			
			spot.addEventListener(MouseEvent.CLICK, showHiddenTwitter);
			
			about.useHandCursor= true;
			about.buttonMode= true;
			about.mouseChildren= false;
			about.addEventListener(MouseEvent.CLICK, mouseHandler);			
			
			evtDsp.dispatchEvent(new TUEvent(CommandType.LOAD_APP_DATA, {twitterView: twitterView, mapView: mapView, skypeView: skypeView, altTwitterView: altTwitterView}, this));
			
			var videoLink:MovieClip= aboutPage.getChildByName("linkInstance") as MovieClip;
			videoLink.useHandCursor= true;
			videoLink.buttonMode= true;
			videoLink.mouseChildren= false;
			videoLink.addEventListener(MouseEvent.CLICK, gotoVideo);
			
			setAllVisible(false);
		}
		
		public function onReady():void
		{
			setAllVisible(true);
			passwordView.visible= false;
			altTwitterView.visible= false;
			aboutPage.visible= false;
			
			TweenLite.to(loading, 0.5, {alpha: 0, onComplete: startAnimation()});
		}
		
		private function gotoVideo(e:MouseEvent):void
		{
			navigateToURL(new URLRequest("http://bit.ly/airql8"), "_blank");
		}
		
		private function mouseHandler(e:MouseEvent):void
		{
			if(!aboutPage.visible){
				aboutPage.visible= true;
				aboutPage.scaleX= 0;
				TweenLite.to(aboutPage, 0.5, {scaleX: 1});
			} else {
				TweenLite.to(aboutPage, 0.5, {scaleX: 0, onComplete: function(){ aboutPage.visible= false; }});
			}
		}
		
		private function setAllVisible(value:Boolean):void
		{
			sobres.visible= value;
			about.visible= value;
			polaroid.visible= value;
			foto.visible= value;
			aboutPage.visible= value;
			passwordView.visible= value;
			calendarView.visible= value;
			altTwitterView.visible= value;
			twitterView.visible= value;
			mapView.visible= value;
			skypeView.visible= value;
		}
		
		private function startAnimation():void
		{
			loading.visible= false;
			
			sobres.scaleX= 0;
			sobres.scaleY= 0;
			mapView.scaleX= 0;
			mapView.scaleY= 0;
			TweenLite.to(sobres, 0.3, {scaleX: 1, scaleY: 1, easing: Elastic.easeInOut});
			TweenLite.to(mapView, 0.3, {scaleX: 1, scaleY: 1, easing: Elastic.easeInOut});
			
			var polY:Number= polaroid.y;
			var fotY:Number= foto.y;
			var skyY:Number= skypeView.y;
			polaroid.y-= 400;
			foto.y-= 400;
			skypeView.y-= 400;
			TweenLite.to(polaroid, 0.4, {y: polY, delay: 0.3, easing: Strong.easeOut});
			TweenLite.to(foto, 0.4, {y: fotY, delay: 0.3, easing: Strong.easeOut});
			TweenLite.to(skypeView, 0.4, {y: skyY, delay: 0.3, easing: Strong.easeOut});
			
			
			calendarView.scaleX= 0;
			calendarView.scaleY= 0;
			TweenLite.to(calendarView, 0.3, {scaleX: 1, scaleY: 1, delay: 0.7, easing: Elastic.easeInOut});
			
			var twtX:Number= twitterView.x;
			var twtY:Number= twitterView.y;
			twitterView.x= 720;
			twitterView.y= -170;
			TweenLite.to(twitterView, 0.5, {x:twtX, y:twtY, rotation:360, delay: 1, ease:Linear.easeNone});
			
			about.alpha= 0;
			TweenLite.to(about, 0.3, {alpha: 1, delay: 1.5});
		}
		
		private function showHiddenTwitter(e:MouseEvent):void
		{
			passwordView.addEventListener(Event.ACTIVATE, onPassActivate);
			passwordView.visible= true;
			passwordView.scaleX= 0;
			passwordView.scaleY= 0;
			TweenLite.to(passwordView, 0.5, {scaleX: 1, scaleY: 1, easing: Bounce.easeInOut});
		}
		
		private function onPassActivate(e:Event):void
		{
			passwordView.removeEventListener(Event.ACTIVATE, onPassActivate);
			altTwitterView.visible= true;
			altTwitterView.scaleX= 0;
			altTwitterView.scaleY= 0;
			TweenLite.to(altTwitterView, 0.5, {scaleX: 1, scaleY: 1, easing: Bounce.easeInOut});
		}
	}
}